{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## A langchain example\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "29e1190379d32b64"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/plain": "<IPython.core.display.HTML object>",
      "text/html": "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": ""
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_community.chat_models import ChatTongyi\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.schema import AIMessage, HumanMessage\n",
    "import gradio as gr\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    openai_api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model_name=\"qwen-max\",\n",
    "    temperature=0.7,\n",
    ")\n",
    "\n",
    "\n",
    "# llm = ChatTongyi(streaming=True)\n",
    "\n",
    "\n",
    "def predict(message, history):\n",
    "    history_langchain_format = []\n",
    "    for msg in history:\n",
    "        if msg['role'] == \"user\":\n",
    "            history_langchain_format.append(HumanMessage(content=msg['content']))\n",
    "        elif msg['role'] == \"assistant\":\n",
    "            history_langchain_format.append(AIMessage(content=msg['content']))\n",
    "    history_langchain_format.append(HumanMessage(content=message))\n",
    "    gpt_response = llm(history_langchain_format)\n",
    "    return gpt_response.content\n",
    "\n",
    "\n",
    "gr.ChatInterface(predict, type=\"messages\").launch()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-18T02:28:31.304937Z",
     "start_time": "2024-11-18T02:28:24.645269Z"
    }
   },
   "id": "efbce01bfff9fa5c",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "## A streaming example using openai\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d8467c94d7be73d0"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'ChatTongyi' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[1], line 9\u001B[0m\n\u001B[1;32m      5\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mgradio\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mgr\u001B[39;00m\n\u001B[1;32m      7\u001B[0m load_dotenv()\n\u001B[0;32m----> 9\u001B[0m llm2 \u001B[38;5;241m=\u001B[39m ChatTongyi(streaming\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m)\n\u001B[1;32m     12\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mpredict\u001B[39m(message, history):\n\u001B[1;32m     13\u001B[0m     history_openai_format \u001B[38;5;241m=\u001B[39m []\n",
      "\u001B[0;31mNameError\u001B[0m: name 'ChatTongyi' is not defined"
     ]
    }
   ],
   "source": [
    "from langchain_community.llms.tongyi import Tongyi\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from langchain.schema import AIMessage, HumanMessage\n",
    "import gradio as gr\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "llm2 = ChatTongyi(streaming=True)\n",
    "\n",
    "\n",
    "def predict(message, history):\n",
    "    history_openai_format = []\n",
    "    for msg in history:\n",
    "        history_openai_format.append(msg)\n",
    "    history_openai_format.append(message)\n",
    "\n",
    "    response = llm2.chat.completions.create(model='qwen-max',\n",
    "                                            messages=history_openai_format,\n",
    "                                            temperature=1.0,\n",
    "                                            stream=True)\n",
    "\n",
    "    partial_message = \"\"\n",
    "    for chunk in response:\n",
    "        if chunk.choices[0].delta.content is not None:\n",
    "            partial_message = partial_message + chunk.choices[0].delta.content\n",
    "            yield partial_message\n",
    "\n",
    "\n",
    "gr.ChatInterface(predict, type=\"messages\").launch()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-14T10:08:40.497761Z",
     "start_time": "2024-11-14T10:08:34.195601Z"
    }
   },
   "id": "d798c41d4922f16e",
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
